﻿//popup
$.fn.popupWin = function(){
	//var box = $(this);
	$(this).click(function(){
		var id = $(this).attr('name'),
		target = $(this),
		wWidth = $(window).width(),
		wHeight = $(window).height();

		function createWin(){
			var copy = $('#imWin').clone().attr('id',id).show();
			
			copy.drag(copy.find('.winTop')).find('.winTop strong').html(target.html()+'<'+id+'>');
			copy.find('.closeBt').click(function(){
				copy.hide();
			});
			copy.find('.sendBt').click(function(){
				var from = $('#infoBar strong').attr('name'),
				to = copy.attr('id'),
				msg = copy.find('textarea').val();
				sendMsg(from, to, msg);
			});
			$(document.body).append(copy);
			copy.css({left:(wWidth / 2 - copy.width() / 2)+'px',top:(wHeight / 2 - copy.height() / 2)+'px'});
		}
		
		if($('#popupStyle').length == 0){
			var css = $('<link id="popupStyle" type="text/css" rel="stylesheet" href="./css/popup.css" />');
			$('head').append(css);
		}
		if($('#imWin').length == 0){
			$('#tempBox').load('./template/imWin.html',function(){
				createWin();
			});
		}else{
			if($('#'+id).length == 0){
				createWin();
			}else{
				$('#'+id).show();
			}
		}
		return false;
	});
	return $(this);
}
//drag
$.fn.drag = function(handle,fn){
	var locked = true, mouseX, mouseY, localX, localY, x, y;
	var box = $(this);
	var handle = handle ? handle : $(this);
	$.fn.drag.zindex = 200;
	
	box.mousedown(function(){
		box.css('zIndex',++$.fn.drag.zindex);
	});
	
	handle.mousedown(function(e){
		locked = false;
		localX = e.layerX || e.offsetX;
		localY = e.layerY || e.offsetY;
		//box.css('zIndex',++zindex);
		//return false;
	})
	.mousemove(function(e){
		if(!locked){
			mouseX = e.clientX;
			mouseY = e.clientY;
			x = mouseX - localX;
			y = mouseY - localY;
			box.css({top:y+'px', left:x+'px'});
		}
		return false;
	});
	$(document.body).mouseup(function(){
		locked = true;
	});
	return $(this);
}